import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common';
import { catchError, Observable, throwError } from 'rxjs';

@Injectable()
export class CatchErrorTestInterceptor implements NestInterceptor {
  private readonly logger = new Logger(CatchErrorTestInterceptor.name)

  intercept (context: ExecutionContext, next: CallHandler): Observable<any> {
    
    //执行结果（Observable）会传递给pipe()函数，做进一步处理。
    return next.handle().pipe(

      catchError(err => {
        this.logger.error(err.message, err.stack) //打印日志信息
        return throwError(() => err)
      })
      
    )
  }
}